﻿using ZYCommon;
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using Bifrost;

namespace TextEditor
{

    /// <summary>
    /// 文本块属性设置
    /// </summary>
    public class DivTagProvider : SmartTagProvider
    {
        public override void GetCommands(ArrayList myList)
        {
            ZYTextDiv parent = base.OwnerDocument.Content.CurrentElement.Parent as ZYTextDiv;
            if (parent != null)
            {
                // myList.Add(new SmartTagItem(this, 0, "设置编号"));
                myList.Add(new SmartTagItem(this, 1, "设置标题"));
                if (parent.timeTitle != "Y")
                {
                    myList.Add(new SmartTagItem(this, 2, "标题字体"));
                    if (base.OwnerDocument.Us.InpatientInfo == null)
                    {
                        myList.Add(new SmartTagItem(this, 14, parent.CanDelete, "锁定文本块"));
                        myList.Add(new SmartTagItem(this, 16, parent.IsPrintVisible, "浏览打印不显示"));
                    }
                    myList.Add(new SmartTagItem("-"));
                    myList.Add(new SmartTagItem(this, 3, parent.HideTitle, "隐藏标题"));
                    //myList.Add(new SmartTagItem(this, 4, parent.NoContent, "不允许包含内容"));
                    myList.Add(new SmartTagItem(this, 5, parent.TitleLine, "标题占据一行"));
                    myList.Add(new SmartTagItem("-"));
                    myList.Add(new SmartTagItem(this, 7, parent.TitleLeftAlign, "标题左对齐"));
                    myList.Add(new SmartTagItem(this, 8, parent.TitleCenterAlign, "标题居中对齐"));
                    myList.Add(new SmartTagItem(this, 9, parent.TitleRightAlign, "标题右对齐"));
                    myList.Add(new SmartTagItem(this, 10, "向右移50px"));
                    myList.Add(new SmartTagItem(this, 11, "原始位置"));
                    myList.Add(new SmartTagItem("-"));
                    //if (parent.timeTitle != "Y")
                    //    myList.Add(new SmartTagItem(this, 15, parent.Visible, "隐藏文本块"));
                    myList.Add(new SmartTagItem(this, 13, "向上插入文本块"));
                    myList.Add(new SmartTagItem(this, 12, "向下插入文本块"));
                    myList.Add(new SmartTagItem("-"));
                    if (!parent.CanDelete && parent.timeTitle != "Y")
                        myList.Add(new SmartTagItem(this, 6, "删除本文本块"));
                }
            }
        }

        public override Point GetPos()
        {
            ZYTextDiv parent = base.OwnerDocument.Content.CurrentElement.Parent as ZYTextDiv;
            if (parent != null)
            {
                return new Point(-base.OwnerControl.SmartTagSize() - 13, parent.RealTop);
            }
            return Point.Empty;
        }

        public override bool HandleCommand(SmartTagItem item)
        {
            ZYTextDiv parent = base.OwnerDocument.Content.CurrentElement.Parent as ZYTextDiv;
            if (parent != null)
            {
                switch (item.ID)
                {
                    case 0:
                        string strData = dlgInputBox.InputBox("输入新的编号", "系统输入", parent.ID);
                        if (StringCommon.isBlankString(strData))
                        {
                            break;
                        }
                        parent.ID = strData.Trim();
                        base.OwnerDocument.Modified = true;
                        break;
                    case 1:
                        /****
                         *时间标题
                         */
                        if (parent.timeTitle == "Y")
                        {
                            if (base.OwnerDocument.Us.TextKind_id == 127 || base.OwnerDocument.isSjyscf)
                            {
                                //if (base.OwnerDocument.HaveSuperiorSign(true))
                                //{
                                //    App.Msg("请先撤销上级医师签名在修改标题！");
                                //    return true;
                                //}
                                using (Bifrost.frmUpdateTime ft = new Bifrost.frmUpdateTime(DateTime.Now.ToString(), "查房记录", true))
                                {
                                    ft.ShowDialog();
                                    if (Bifrost.App.CurrentHightDoctorId == "")
                                    {
                                        return true;
                                    }
                                    base.OwnerDocument.Us.RecordText = Bifrost.App.CurrentUpdateContent;
                                    base.OwnerDocument.Us.RecordTime = Bifrost.App.CurrentUpdateTime;
                                    base.OwnerDocument.SuporSign = Bifrost.App.CurrentHightDoctorId;
                                    parent.Title = Bifrost.App.CurrentUpdateTime + "   " + Bifrost.App.CurrentUpdateContent;
                                }
                            }
                            else
                            {
                                using (frmUpdateTime ft = new frmUpdateTime(parent.Title, base.OwnerDocument.HidleNameTitle))
                                {
                                    //if (base.OwnerDocument.Us.RecordTime != "")
                                    //{
                                    //    ft = new frmUpdateTime(base.OwnerDocument.Us.RecordTime, base.OwnerDocument.Us.RecordText, base.OwnerDocument.HidleNameTitle);
                                    //}
                                    //else
                                    //{

                                    //}
                                    if (ft.ShowDialog() == DialogResult.OK)
                                    {
                                        base.OwnerDocument.Us.RecordText = ft.TxtContent;
                                        base.OwnerDocument.Us.RecordTime = ft.TimeString;
                                        parent.Title = ft.TimeString + "   " + ft.TxtContent;
                                    }
                                }
                            }
                        }
                        /***
                         * 普通标题
                         */
                        else
                        {
                            string str2 = dlgInputBox.InputBox("输入新的标题", "系统输入", parent.Title);
                            if (str2 == null)
                            {
                                break;
                            }
                            parent.Title = str2;
                        }
                        base.OwnerDocument.Modified = true;
                        break;
                    case 2:
                        FontDialog dialog = new FontDialog();
                        dialog.Font = parent.Font;
                        if (dialog.ShowDialog() != DialogResult.OK)
                        {
                            dialog.Dispose();
                            break;
                        }
                        parent.Font = dialog.Font;
                        base.OwnerDocument.Modified = true;
                        dialog.Dispose();
                        break;
                    case 3:
                        parent.HideTitle = !parent.HideTitle;
                        base.OwnerDocument.Modified = true;
                        break;
                    case 4:
                        parent.NoContent = !parent.NoContent;
                        base.OwnerDocument.Modified = true;
                        break;
                    case 5:
                        parent.TitleLine = !parent.TitleLine;
                        parent.TitleLeftAlign = true;
                        base.OwnerDocument.Modified = true;
                        break;
                    case 6:
                        if (base.OwnerDocument.DocumentElement != parent)
                        {
                            parent.Parent.RemoveChild(parent);
                            base.OwnerDocument.Modified = true;
                            base.OwnerControl.UpdateSmartTag();
                        }
                        break;
                    case 7:
                        parent.TitleLeftAlign = true;
                        base.OwnerDocument.Modified = true;
                        base.OwnerDocument.Refresh();
                        break;
                    case 8:
                        parent.TitleCenterAlign = true;
                        base.OwnerDocument.Modified = true;
                        break;
                    case 9:
                        parent.TitleRightAlign = true;
                        parent.TitleLine = true;
                        base.OwnerDocument.Modified = true;
                        break;
                    case 10:
                        parent.MarginLeft += 50;
                        base.OwnerDocument.Modified = true;
                        break;
                    case 11:
                        parent.MarginLeft = 0;
                        base.OwnerDocument.Modified = true;
                        break;
                    case 12:
                        base.OwnerDocument._InsertDivDown(parent, 0); //向下插入文本块
                        break;
                    case 13:
                        base.OwnerDocument._InsertDivDown(parent, 1); //向上插入文本块
                        break;
                    case 14:
                        parent.CanDelete = !parent.CanDelete;
                        ((ZYTextContainer)parent).bolChildElementsLocked = parent.CanDelete;
                        break;
                    case 15:
                        parent.Visible = !parent.Visible;
                        if (parent.Visible == true)
                        {
                            foreach (ZYTextElement ze in parent.ChildElements)
                            {
                                if (ze is ZYTextDiv)
                                {
                                    ZYTextDiv zd = (ZYTextDiv)ze;
                                    if ((base.OwnerDocument.isDiagnoseName(zd.Title)))
                                    {
                                        ((ZYTextDiv)ze).ChildElements.Clear();
                                    }
                                }
                                if (ze is ZYTextInput)
                                {
                                    ZYTextInput zi = (ZYTextInput)ze;
                                    if (zi.Text.Contains("签名"))
                                    {
                                        zi.Text = "";
                                    }
                                }
                            }
                        }
                        base.OwnerDocument.Modified = true;
                        break;
                    case 16:
                        parent.IsPrintVisible = !parent.IsPrintVisible;
                        break;
                }
                base.OwnerDocument.ContentChanged();
                base.OwnerDocument.UpdateView();
                return true;
            }
            return false;
        }

        public override bool isEnable()
        {
            if ((base.OwnerDocument.Content.SelectLength == 0) && (base.OwnerDocument.Content.CurrentElement != null))
            {
                ZYTextDiv parent = base.OwnerDocument.Content.CurrentElement.Parent as ZYTextDiv;

                if (base.OwnerDocument.Content.PreElement != null)
                {
                    if (((parent != null)
                        && (parent != base.OwnerDocument.DocumentElement))
                        && base.OwnerDocument.Content.PreElement.Parent != parent)
                    {
                        return true;
                    }
                }
                else
                {
                    if (((parent != null) && (parent != base.OwnerDocument.DocumentElement)) && base.OwnerDocument.Content.PreElement == null)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public override int ImageIndex
        {
            get
            {
                return 0x10;
            }
        }
    }
}

